package com.runjian.rundo.iot.agent.iec104.parser.frame.control.process;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.runjian.rundo.iot.agent.iec104.consts.Ti;
import com.runjian.rundo.iot.agent.iec104.parser.frame.ASDUFrame;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ByteUtil;
import com.runjian.rundo.iot.agent.iec104.parser.frame.control.base.TaControl;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;

/**
 * C_SE_NB_1(49, "遥调设点,标度化(参数设置)"),
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class C_SE_NB_1 extends TaControl {

    /**
     * 初始化处理
     */
    @Override
    public void init() {
        super.init();
        // 类型
        this.setTi(Ti.C_SE_NB_1);
    }


    @Override
    public void analysisInfo(byte[] msg) {
        this.addInfoElement(InfoElement.<Short>builder().address(ByteUtil.bytesToInt(new byte[] {msg[12], msg[13], msg[14], 0x00}))
            .value(ByteUtil.bytesToShort(ArrayUtil.sub(msg, 15, 17))).build());
        this.setQos((short)(msg[17] & 0xff));
    }

    @Override
    public void addControlInfoElement(int address, Object value) {
        if (!StrUtil.isNumeric(value.toString())) {
            Assert.isTrue(Boolean.FALSE, "值必须是短整型数字");
        }
        super.addControlInfoElement(address,value);
    }


    /**
     * 写入byteBuf
     * @param assembleByteBuf 字对缓存对象
     * @param infoElement 信息对象
     */
    public void writeInfoContent(ByteBuf assembleByteBuf, InfoElement<Object> infoElement) {
        assembleByteBuf.writeBytes(ByteUtil.shortToBytes((short)infoElement.getValue()));
    }


}
